package com.example.coreshopapp.task;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.example.coreshopapp.app.Constants;
import com.example.coreshopapp.bean.AddMemberReq;
import com.example.coreshopapp.http.AccessNet;
import com.google.gson.Gson;

import java.util.HashMap;

public class MemberTask {
    private Handler searchOutHandler;
    public void setSearchOutHandler(Handler searchOutHandler) {
        this.searchOutHandler = searchOutHandler;
    }
    private HashMap<String,String> search;
    @SuppressLint("HandlerLeak")
    private Handler searchHandler = new Handler() {
        //        @SuppressLint("HandlerLeak")
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Message message = searchOutHandler.obtainMessage();
            switch (msg.what) {
                case 9900:
                    message.what = 9900;
                    message.obj = msg.obj;// info;
                    searchOutHandler.sendMessage(message);
                    break;
                case 9901:
                    message.what = 9901;
                    message.obj = msg.obj;// info;
                    Log.d("用户登录50010,", msg.obj.toString() + "");
                    searchOutHandler.sendMessage(message);
                    break;
                case 9902:
                    message.what = 9901;
                    message.obj = "操作异常，请重试";// info;
                    searchOutHandler.sendMessage(message);
                    break;
                case  9904:
                    message.what = 9904;
                    message.obj = msg.obj;// info;
                    searchOutHandler.sendMessage(message);
                    break;
            }
        }
    };
    private boolean searchFreshNetData() {
        new Thread(() -> {
            Gson gson = new Gson();
            boolean re = AccessNet.postDataNet( searchHandler, Constants.urlClass.search_member, 6,gson.toJson(search)); // 调用接口
        }).start();
        return true;
    }
    public Boolean execute(HashMap<String,String> search) {
        this.search = search;
        return searchFreshNetData();
    }

    // 新增
    private Handler addOutHandler;
    public void setAddOutHandler(Handler addOutHandler) {
        this.addOutHandler = addOutHandler;
    }
    private AddMemberReq addMemberReq;
    @SuppressLint("HandlerLeak")
    private Handler addHandler = new Handler() {
        //        @SuppressLint("HandlerLeak")
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Message message = addOutHandler.obtainMessage();
            switch (msg.what) {
                case 9900:
                    message.what = 9900;
                    message.obj = msg.obj;// info;
                    addOutHandler.sendMessage(message);
                    break;
                case 9901:
                    message.what = 9901;
                    message.obj = msg.obj;// info;
                    Log.d("用户登录50010,", msg.obj.toString() + "");
                    addOutHandler.sendMessage(message);
                    break;
                case 9902:
                    message.what = 9901;
                    message.obj = "操作异常，请重试";// info;
                    addOutHandler.sendMessage(message);
                    break;
                case  9904:
                    message.what = 9904;
                    message.obj = msg.obj;// info;
                    addOutHandler.sendMessage(message);
                    break;
            }
        }
    };

    private boolean addFreshNetData() {
        new Thread(() -> {
            Gson gson = new Gson();
            boolean re = AccessNet.postDataNet( addHandler, Constants.urlClass.add_member, 6,gson.toJson(addMemberReq)); // 调用接口
        }).start();
        return true;
    }
    public Boolean addExecute( AddMemberReq addMemberReq) {
        this.addMemberReq = addMemberReq;
        return addFreshNetData();
    }

}
